#include "FanRpm.h"
#include "Timer1.h"

#define us_in_min 60000000

volatile uint32_t rpm;
volatile uint16_t ticksdiff;

void FanRpm_init(void)
{
	fan_channel = 0;
	CLEAR_BIT(DDRD, PD2);
	EICRA |= (1<<ISC21) | (0<<ISC20);
	EIMSK |= (1<<INT2);

	SET_BIT(DDRD, PD6);
	SET_BIT(DDRD, PD7);
	
	SET_BIT(PORTD, PD6);
	SET_BIT(PORTD, PD7);


}

void FanRpm_setChannel(uint8_t channel)
{
	// TODO: switch Multiplexer to corresponding channel

}

void CalcRpm(void)
{/*
	if(TIMER1_OVERFLOW)
		ticksdiff = (65536 - timer1_ticks_old) + (timer1_ticks_new - TIMER1_OFFSET);
	else
		ticksdiff = timer1_ticks_old - timer1_ticks_new;

	rpm = (2 * us_in_min) / (ticksdiff);

	switch(fan_channel)
	{
		case 0:
			s_data.fanrpm_v0 = (uint16_t)rpm;
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;

	};
*/

}


ISR (INT2_vect)
{
	timer1_ticks_old = timer1_ticks_new;
	timer1_ticks_new = readTCNT1();
}



